package com.xyzboom.web.filters;

import jakarta.servlet.*;
import jakarta.servlet.annotation.*;
import jakarta.servlet.http.HttpServletRequest;

import java.io.IOException;

/**
 * 用于将所有页面正确编码为中文的过滤器
 */
@WebFilter(filterName = "EnCodeFilter",
        urlPatterns = {"/*"})
public class EncodeFilter implements Filter {
    public void init(FilterConfig config) {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        if (!httpServletRequest.getServletPath().endsWith(".css")) {
            response.setContentType("text/html;charset=UTF-8");
        }
        chain.doFilter(request, response);
    }
}
